package org.vsg.stock.basic.service.volfigure;

import java.text.DecimalFormat;

import org.vsg.stock.core.domain.StockDailyKLine;

/**
 * 定义参照价格的标准,以参照量柱
 * @author Bill Vison
 *
 */
public class PriceReferenceLevel {
	
	
	public static final short LOW_TO_OPEN = 3;
	
	public static final short OPEN_TO_MIDDLE= 4;

	public static final short MIDDLE_TO_CLOSE = 5;
	
	public static final short CLOSE_TO_HIGH = 6;
	
	public static final short OVER_HIGH = 7;

	public static final short LAST_CLOSE_TO_LOW = 2;	

	public static final short AT_LOWEST = 1;
	
	public static final short UNKNOW  = 0 ;
	
	private StockDailyKLine standarKLine;
	
	private double lastClosePrice;

	static private DecimalFormat nf = new DecimalFormat("##0.000");	
	
	public PriceReferenceLevel(  StockDailyKLine standarKLine , double  lastClosePrice) {
		this.standarKLine = standarKLine;
		this.lastClosePrice = lastClosePrice;
	}
	
	public short checkLevelValue(StockDailyKLine checkKLine , short klineFields) {
		
		double compareValue = checkKLine.getMiddle();
		switch (klineFields) {
			case StockDailyKLine.CLOSE:
				compareValue = checkKLine.getClose();
				break;
			case StockDailyKLine.OPEN:
				compareValue = checkKLine.getOpen();
				break;
			case StockDailyKLine.LOW:
				compareValue = checkKLine.getLow();
				break;
			case StockDailyKLine.HIGH:
				compareValue = checkKLine.getHigh();
				break;
			case StockDailyKLine.MIDDLE:
				compareValue = checkKLine.getMiddle();
				break;			
			default:
		}
		
		
		// --- set the max & min value
		double greatValue = standarKLine.getOpen() > standarKLine.getClose() ? standarKLine.getOpen() : standarKLine.getClose();
		double lessValue = standarKLine.getClose() < standarKLine.getOpen() ? standarKLine.getClose() : standarKLine.getOpen();
		
		// --- check the value area ---
		if ( compareValue >= standarKLine.getLow() 
				&& compareValue < lessValue ) {
			return LOW_TO_OPEN;
		}
		else if (compareValue >= lessValue 
				&& compareValue < standarKLine.getMiddle() ) {
			return OPEN_TO_MIDDLE;
		}
		else if (compareValue >= standarKLine.getMiddle() 
				&& compareValue < greatValue ) {
			return MIDDLE_TO_CLOSE;
		}
		else if (compareValue >= greatValue 
				&& compareValue < standarKLine.getHigh() ) {
			return CLOSE_TO_HIGH;
		}
		else if (compareValue >= standarKLine.getHigh() ) {
			return OVER_HIGH;
		}
		
		// ---- count the rate ---
		if ( lastClosePrice < standarKLine.getLow() ) {
			// --- 以前天的收盘价为底线 ----
			if (compareValue >= lastClosePrice
				&& compareValue < standarKLine.getLow() ) {
				return LAST_CLOSE_TO_LOW;
			}
		}
		else {
			double lowest = standarKLine.getLow() * 0.995;
			if (compareValue >= lowest
					&& compareValue < standarKLine.getLow() ) {
					return AT_LOWEST;
			}			
		}
	
		
		return UNKNOW;
	}

}
